﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：ApiHttpClient.cs
// 功能描述：TongTongMall.MallWeb.ApiRepository
// 
// Create User：jym 2017/02/28
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using TongTongMall.Dependecy;

namespace TongTongMall.MallWeb.ApiRepository.Domain
{
    public class ApiHttpClient : IApiHttpClient, ISingletonDependency
    {
        public TimeSpan DefaultTimeout { get; set; }

        public ApiHttpClient()
        {
            this.DefaultTimeout = TimeSpan.FromSeconds(5);
        }

        /// <summary>
        /// Post url 
        /// 参数按照application/x-www-form-urlencoded传递
        /// </summary>
        /// <param name="apiUri">post地址</param>
        /// <param name="httpContents">参数</param>
        /// <returns></returns>
        public async Task<HttpResponseMessage> PostAsync(Uri apiUri, IDictionary<string, StringContent> httpContents)
        {
            using (var client = new HttpClient())
            {
                client.Timeout = DefaultTimeout;
                client.DefaultRequestHeaders.ExpectContinue = false;
                using (var content = new MultipartFormDataContent())
                {
                    if (httpContents != null)
                    {
                        foreach (var httpContent in httpContents)
                        {
                            content.Add(httpContent.Value, httpContent.Key);
                        }
                    }
                    //预热连接
                    await client.SendAsync(new HttpRequestMessage
                    {
                        Method = new HttpMethod("HEAD"),
                        RequestUri = apiUri
                    });
                    return await client.PostAsync(apiUri, content).ConfigureAwait(false);
                }
            }
        }

        /// <summary>
        /// POST url
        /// </summary>
        /// <param name="apiUri">post地址</param>
        /// <param name="content">post内容</param>
        /// <param name="timeout">相应超时时间，为空默认90秒</param>
        /// <returns></returns>
        public async Task<HttpResponseMessage> PostAsync(string apiUri, object content, int? timeout = null)
        {
            using (var client = new HttpClient())
            {
                client.Timeout = timeout != null ? TimeSpan.FromMilliseconds(timeout.Value) : DefaultTimeout;
                client.DefaultRequestHeaders.ExpectContinue = false;
                //预热连接
                await client.SendAsync(new HttpRequestMessage
                {
                    Method = new HttpMethod("HEAD"),
                    RequestUri = new Uri(apiUri)
                });
                using (var requestContent = content is HttpContent ? (HttpContent)content : new StringContent(
                    $"{content}"))
                {
                    return await client.PostAsync(apiUri, requestContent).ConfigureAwait(false);
                }
            }
        }
    }
}
